"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
"""
from dao.plan_dao import PlanDao
from do.plan_do import PlanDo
from do.testcase_do import TestcaseDo
from service.testcase_service import TestcaseService

plan_dao = PlanDao()
testcase_service = TestcaseService()

class PlanService:

    def get(self, testcase_id):
        # 通过id查询数据
        return plan_dao.get(testcase_id)

    def list(self):
        # 返回所有
        return plan_dao.list()

    def save(self, testcase_id_list, plan_do: PlanDo):
        # 保存之前先查询数据是否存在
        # testcase_id_list  [1,2,3,4] ==[TestcaseDo1, TestcaseDo2, TestcaseDo3, TestcaseDo4]
        # 存在返回false,不存在则进行新增
        plan = self.get_by_name(plan_do.name)
        if not plan:
            testcase_do_list = [testcase_service.get(testcase_id) for testcase_id in testcase_id_list]
            plan_do.testcases = testcase_do_list
            return plan_dao.save(plan_do)
        else:
            return False

    # def update(self, testcase_do: TestcaseDo):
    #     # 用例存在则进行更新修改，否则返回错误
    #     testcase = self.get(testcase_do.id)
    #     if not testcase:
    #         return False
    #     else:
    #         return plan_dao.update(testcase_do)

    def delete(self, plan_id):
        # 用例存在则进行删除，否则返回错误
        plan = self.get(plan_id)
        if not plan:
            return False
        else:
            return plan_dao.delete(plan_id)

    def get_by_name(self, name):
        return plan_dao.get_by_name(name)
